package org.opsli.modulars.common;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.opsli.api.base.result.ResultWrapper;
import org.opsli.core.utils.DYCUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * 微信公众号登录相关控制器
 */
@Api(tags = "请求腾讯地图api")
@Slf4j
@RestController
@RequestMapping("/weMini/NotToken/Map")

public class apisMapConrroller {

    // TODO 腾讯地图api
    @ApiOperation(value = "登录", notes = "小程序登录")
    @GetMapping("/get")
    public ResultWrapper<?> getopenid(String lat,String lon) {
        Map<String,String> map = new HashMap<>();
        map.put("lat",lat);
        map.put("lon",lon);
        String ckey = "MapCache##"+lat+","+lon;
        Object cache = DYCUtils.get(ckey);

        /**
         * 有缓存就使用缓存
         */
        Object data = null;
        if (cache == null){
            String key = "7WVBZ-KCTCF-JXIJD-JDXRH-SDHCQ-KKF5J";
            String url = "https://apis.map.qq.com/ws/geocoder/v1/?coord_type=5&get_poi=0&output=json&key="+key+"&location="+lat+","+lon;
            String result = HttpUtil.get(url);
            data= JSONUtil.parse(result);
            DYCUtils.set(ckey, data, 60 * 60, TimeUnit.MINUTES);
        }else {
            data = cache;
            log.info("位置使用了缓存{}",ckey);
        }

        return ResultWrapper.getSuccessResultWrapper(data);
    }
}
